package com.atguigu.juc.study.chapter10.tl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : leimingchao
 * @date Date : 2022年06月19日 17:01
 */
public class ThreadLocalDemo2 {

    public static void main(String[] args) {

        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        MyData myData = new MyData();

        try {

            for (int i = 0; i < 10; i++) {
                threadPool.submit(() -> {
                    try {
                        Integer beforeInt = myData.threadLocal.get();
                        myData.add();
                        Integer afterInt = myData.threadLocal.get();
                        System.out.println(Thread.currentThread().getName() + "\t" + "beforeInt:" + beforeInt + "\t" + "afterInt:" + afterInt);
                    } finally {
                        myData.threadLocal.remove();
                    }
                });
            }

        } catch (Exception e) {

        } finally {
            threadPool.shutdown();
        }
    }
}
